package com.bootdo.common.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.dao.FileDao;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileAppsService;
import com.bootdo.common.service.FileService;
import com.smart.common.utils.FileType;
import com.smart.common.utils.FileUtil;

@Service
public class FileServiceImpl implements FileService {
	@Autowired
	private FileDao sysFileMapper;
	
	@Autowired
	private FileAppsService fileAppsService;

	@Autowired
	private BootdoConfig bootdoConfig;

	@Override
	public FileDO get(Long id) {
		return sysFileMapper.get(id);
	}

	@Override
	public List<FileDO> list(Map<String, Object> map) {
		return sysFileMapper.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return sysFileMapper.count(map);
	}

	@Override
	public int save(FileDO sysFile) {
		return sysFileMapper.save(sysFile);
	}

	@Override
	public int update(FileDO sysFile) {
		return sysFileMapper.update(sysFile);
	}

	@Override
	public int remove(Long id) {
		return sysFileMapper.remove(id);
	}

	@Override
	public int batchRemove(Long[] ids) {
		return sysFileMapper.batchRemove(ids);
	}

	@Override
	public Boolean isExist(String url) {
		Boolean isExist = false;
		if (!StringUtils.isEmpty(url)) {
			String filePath = url.replace("/files/", "");
			filePath = bootdoConfig.getUploadPath() + filePath;
			File file = new File(filePath);
			if (file.exists()) {
				isExist = true;
			}
		}
		return isExist;
	}

	@Override
	public FileDO saveUploadFile(MultipartFile file, String custFileName) {
		 String originalFilename = file.getOriginalFilename();
	     String fileName = FileUtil.renameToUUID(originalFilename);
	     long size = file.getSize();
	     Date date = new Date();
//	     String fileUrlPrefix = "/files/"+DateUtils.format(date)+"/";
//	     String uploadPath = bootdoConfig.getUploadPath()+DateUtils.format(date)+"/";
	     String fileUrlPrefix = "/files/";
	     String uploadPath = bootdoConfig.getUploadPath();
	     try {
			InputStream inputStream = file.getInputStream();
			
			ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
			byte[] buff = new byte[1024];
			int rc = 0;
			while ((rc = inputStream.read(buff, 0, 100)) > 0) {  
				swapStream.write(buff, 0, rc);
			}
			byte[] in2b = swapStream.toByteArray();
			FileUtil.uploadFile(in2b, uploadPath, fileName);
			
			//TODO 待测试,保存后回写
		    FileDO sysFile = new FileDO(FileType.fileType(fileName), fileUrlPrefix + fileName, date);
		    sysFile.setFileName(originalFilename);
		    String substring = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
		    sysFile.setFileType(substring);
		    //byte size
		    sysFile.setFileSize(new BigDecimal(size));
		    
		    int save = save(sysFile);
		    if(save > 0) {
		    	return sysFile;
		    }
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
	   
		return null;
	}

	@Override
	public int remove(FileDO fileDO) {
		if(fileDO == null) {
			return 0;
		}
		
		String url = fileDO.getUrl();
		String filePath = url.replace("/files/", "");
		filePath = bootdoConfig.getUploadPath() + filePath;
		File file = new File(filePath);
		if (file.exists()) {
			file.delete();
		}
		remove(fileDO.getId());
		return 1;
	}

	@Override
	public List<FileDO> getFormFiles(Integer formId, String formName) {
		return sysFileMapper.getFormFiles(formId,formName);
	}

	@Override
	public int removeFormFile(String id) {
		long fileId = Long.parseLong(id);
		FileDO fileDO = sysFileMapper.get(fileId);
		remove(fileDO);
		
		int removeBySysFileId = fileAppsService.removeBySysFileId(fileId);
		
		return removeBySysFileId;
	}
	
}
